import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/vo/common_vo/common_pagination_vo.dart';
import 'package:xinxuan/vo/user_invitations_vo.dart';

class MyInvitationPageController extends GetxController {
  
  var typeStr = '全部'.obs;
  int type = -1;
  List<Map<String, dynamic>> typeArr = [
    {
      'typeStr': '全部',
      'type': -1
    },
    {
      'typeStr': '已完成',
      'type': 1
    },
    {
      'typeStr': '未完成',
      'type': 0
    }
  ];
  /// 类型下拉选中项
  var typeScrollController = FixedExtentScrollController( initialItem: 0).obs;
  
  /// 获取我的邀请数据
  var userInvitationsVo = UserInvitationsVo(
    data: [],
    meta: UserInvitationsMeta(
      pagination: CommonPagination(
        total: 0,
        currentPage: 1
      )
    )
  ).obs;
  
  @override
  void onReady() {
    getUserInvitations();
    super.onReady();
  }

  /// 获取我的邀请列表
  getUserInvitations() async {
    userInvitationsVo.value.meta!.pagination!.currentPage = 1;
    UserInvitationsVo tempVo = await HttpUtilService.getUserInvitations(isPurchased: type, page: userInvitationsVo.value.meta!.pagination!.currentPage!);
    userInvitationsVo.value = tempVo;
    update();
  }

  /// 获取更多
  onLoadUserInvitations() async {
    if (userInvitationsVo.value.meta!.pagination!.total! <= userInvitationsVo.value.data!.length) {
      return;
    }
    userInvitationsVo.value.meta!.pagination!.currentPage = userInvitationsVo.value.meta!.pagination!.currentPage! + 1;
    UserInvitationsVo tempVo = await HttpUtilService.getUserInvitations(isPurchased: type, page: userInvitationsVo.value.meta!.pagination!.currentPage!);
    userInvitationsVo.value.data!.addAll(tempVo.data!);
    update();
  }

  /// 修改选中类型
  changeTypeIndex(int index) {
    typeScrollController.value = FixedExtentScrollController( initialItem: index);
  }

  /// 确定选中类型
  determineType() {
    typeStr.value = typeArr[typeScrollController.value.initialItem]['typeStr'];
    type = typeArr[typeScrollController.value.initialItem]['type'];
    getUserInvitations();
  }

}